import axios from 'axios'

const isPrd = process.env.NODE_ENV === 'production'
export const basciUrl = isPrd ? '' : ''
const service = axios.create({
  baseURL: basciUrl
})

service.interceptors.request.use(config => {
  if (config.needFormdata) {
    config.headers = {
      'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
      tokenUf: sessionStorage.getItem('token') || ''
    }
  } else {
    config.headers = {
      'Content-Type': 'application/json;charset=utf-8',
      tokenUf: sessionStorage.getItem('token') || ''
    }
  }
  return config
}, error => {
  return error
})

service.interceptors.response.use(response => {
  if (response.data.code) {
    switch (response.data.code) {
      case 200:
        return response.data
      case 302:
        console.log('接口重定向了！');
        return Promise.reject(response.data.msg || 'Error')
      case 400:
        return Promise.reject(console.log(response.data.msg) || 'Error')
      case 401:
        console.log('您未登录，或者登录已经超时，请先登录！')
        return Promise.reject(response.data.msg || 'Error')
      case 403:
        console.log('无权限操作')
        return Promise.reject(response.data.msg || 'Error')
      case 500:
        console.log(response.data.msg)
        return Promise.reject(response.data.msg || 'Error')
      default:
        console.log(response.data.msg)
        return Promise.reject(response.data.msg || 'Error')
    }
  } else {
    return response
  }
})
export default service
